package com.tps.cloud.config;

import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.annotation.Bean;

@AutoConfiguration
public class I18nConfig {

    private static final String BASE_NAME = "classpath:i18n/messages";

    @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource reloadableResourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
        reloadableResourceBundleMessageSource.setBasenames(BASE_NAME);
        reloadableResourceBundleMessageSource.setCacheSeconds(5);
        reloadableResourceBundleMessageSource.setDefaultEncoding("UTF-8");
        return  reloadableResourceBundleMessageSource;
    }

    /**
     * MessageSourceAccessor封装获取消息的方法
     * @param messageSource
     * @return
     */
    @Bean
    public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource) {
        MessageSourceAccessor accessor = new MessageSourceAccessor(messageSource) ;
        return accessor ;
    }

}
